<script setup lang="ts">
import KnowledgeCard from './KonwledgeCard.vue'
import { ref, defineProps } from 'vue'
import type {
  KnowledgeList,
  KnowledgeType,
  KnowledgeParams
} from '@/types/consult.d.ts'
import { getKnowledgePage } from '@/services/consult'

const props = defineProps<{
  type: KnowledgeType
}>()

const loading = ref(false) //瀑布流滚动
const finished = ref(false) //停止滚动
const list = ref<KnowledgeList>([]) //知识列表
const params = ref<KnowledgeParams>({
  //知识列表请求参数
  type: props.type,
  current: 1,
  pageSize: 10
})

// 滚动触底无限加载
const onLoad = async () => {
  //  加载更多
  const res = await getKnowledgePage(params.value)
  list.value.push(...res.data.rows)
  // 判断是否加载完成，停止加载
  if (params.value.current >= res.data.pageTotal) {
    finished.value = true
  } else {
    params.value.current++
  }
  loading.value = false //加载状态
}
</script>

// 知识列表
<template>
  <div class="knowledge-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <knowledge-card v-for="(item, i) in list" :key="i" :item="item" />
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
